<?php
/**
 * 工单每日回复监控
 * User: Administrator
 * Date: 2018\8\10 0010
 * Time: 15:31
 */
namespace app\lib\Order\OrderMonitor;
use app\lib\Order\OrderExtensionConfig\GetOrderBindingExtension;
use app\lib\Order\OrderExtensionConfig\OrderBindingEditExtension;
use app\lib\Order\OrderOption\OrderReplyOption;
use app\model\OrderInfoModel;

class OrderDayReplyMonitor
{
    /**
     * 获取工单数据
     * @return array|bool
     */
    protected function getOrder(){
        $model =  (new OrderInfoModel());
        $map   = [$model->_orderStatus=>1];
        $orderIdArray    = $model->where($map)->column($model->_id);
        if(empty($orderIdArray)){
            return true;
        }
        return $orderIdArray;
    }

    /**
     * 自动回复完毕
     * @param $v int 工单id
     * @param $content string 工地日志内容
     * @param $extensionId int 延期标签
     * @return bool
     * @throws \Exception
     */
    protected function authReply($v,$content,$extensionId){
        $model = (new OrderReplyOption($v,0));
        $model->setExtensionId($extensionId);
        $model->setContent($content);
        $model->save();
        return true;
    }

    /**
     * 生成工单自动回复的内容
     * @param $v int 工单id
     * @return bool|int
     * @throws \Exception
     */
    protected function handle($v){
        $model =  (new GetOrderBindingExtension($v));
        $isExecute = false;
        $content = '';
        $extensionId = 0;
        $result = true;
        if($model->isOrderBindingPay()){
            $isExecute = true;
            $content.='【请按时交纳工单款】';
            $extensionId = 2;
        }
        if($model->isOrderBindingSign()){
            $isExecute = true;
            $content.='【请按时验收】';
            $extensionId = 1;
        }
        if($model->isOrderBindingDaily()){
            if($model->getEndTime()>time()){
                $isExecute = true;
                $content.='【请按时交纳工单款】';
            }else{
                (new OrderBindingEditExtension($v))->autoReleaseBinding();
                $result = $v;
            }
        }
        if($isExecute == true){
            $this->authReply($v,$content,$extensionId);
        }
        return $result;
    }
    /*没有回复的工单提醒回复*/
    protected function sendNotice($data){
        foreach ($data as $v){

        }
    }
    public function save(){
        $data =  $this->getOrder();
        $needId = [];
        foreach ($data as $v){
            $model =  (new GetOrderBindingExtension($v));
            if($model->isOrderBindingExtension()){
                $result =  $this->handle($v);
                if(is_numeric($result) ){
                    $needId[] = $v;
                }
            }
            if(!($model->isOrderBindingExtension())){
                $needId[] = $v;
            }
        }
        if($needId && is_array($needId)){

        }
    }

}